//
//  MHSearchViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/5/14.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSearchViewModel.h"
#import "MHSearchItemViewModel.h"
#import "MHSerchMoment.h"
#import "MHMomentDetailViewModel.h"
@interface MHSearchViewModel ()
@property (nonatomic, readwrite, copy) NSArray *datas;
@end

@implementation MHSearchViewModel

- (void)initialize{
    [super initialize];
    
    @weakify(self);
    
    /// 设置tableView的样式
    self.style = UITableViewStyleGrouped;
    /// 隐藏导航栏的细线
    self.prefersNavigationBarBottomLineHidden = YES;
    
    /// 允许下拉刷新+上拉加载
    self.shouldPullDownToRefresh = NO;
    self.shouldPullUpToLoadMore = YES;
    
    RAC(self, datas) = self.requestRemoteDataCommand.executionSignals.switchToLatest;
    /// 数据源
    RAC(self,dataSource) = [RACObserve(self, datas) map:^(NSArray * datas) {
        @strongify(self)
        return [self dataSourceWithDatas:datas];
    }];
    
    self.didSelectCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        NSIndexPath *indexPath = input;
        MHSerchMoment *searchMoment =  self.datas[indexPath.row];
        if (searchMoment.vedioCount) {
            
            return   [[[self.services.client getRecentInfoByNewVideoWithRecentId:searchMoment.autoId] mh_parsedResults] doNext:^(MHAwemeModel *model) {
                MHAwemeListViewModel *viewModel = [[MHAwemeListViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:@[model],@"MHViewModelShowEnterBtn":@false,@"MHViewModelShowSingleVideo":@true}];
                [self.services pushViewModel:viewModel animated:YES];
            }];
            
        }else
        {
            MHMomentDetailViewModel *detail = [[MHMomentDetailViewModel alloc] initWithServices:self.services params:@{MHViewModelIDKey:@(searchMoment.autoId),MHViewModelIsDetail:@(true)}];
            //        detail.isDetail = YES;
            [self.services pushViewModel:detail animated:YES];
             return [RACSignal empty];
        }
    }];
    self.popCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        [self.services popViewModelAnimated:YES];
        return  [RACSignal empty];
    }];

}

- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page{
    
    NSArray * (^mapDatas)(NSArray *) = ^(NSArray *datas) {
        if (page == 1) {
            /// 下拉刷新
        } else {
            /// 上拉加载
            datas = @[(self.datas ?: @[]).rac_sequence, datas.rac_sequence].rac_sequence.flatten.array;
        }
        return datas;
    };
    
    return [[[self.services.client searchRecentByContentPagedListWithText:self.searchText pageNum:page pageSize:self.perPage circleId:[AXAppDelegate sharedDelegate].currentCircleInfo.autoId] mh_parsedResults] map:mapDatas];
}

#pragma mark - 辅助方法
- (NSArray *)dataSourceWithDatas:(NSArray *)datas {
    if (MHObjectIsNil(datas) || datas.count == 0) return @[];
    
    NSMutableArray *viewModels = [[datas.rac_sequence map:^(MHSerchMoment *moment) {
        MHSearchItemViewModel *viewModel = [[MHSearchItemViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey:moment}];
        return viewModel;
    }].array mutableCopy];
    return viewModels;
}



@end
